package com.swinboo.im.endpoint;

import com.easemob.im.server.EMException;
import com.easemob.im.server.model.EMUser;
import com.huang.entity.result.HCollectionResult;
import com.huang.entity.result.HStringResult;
import com.huang.entity.result.ResponseEnum;
import com.huang.entity.result.ResultBuilder;
import com.huang.exception.IllegalParamException;
import com.swinboo.im.api.EMUserApi;
import com.swinboo.im.entity.param.*;
import com.swinboo.im.service.EMUserService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Swinboo
 * @date 2022-03-17 21:38
 * @description:
 */
@RestController
@Api("")
public class EMUserEndPoint implements EMUserApi {
    @Resource
    private EMUserService emUserService;

    @Override
    public HStringResult create(CreateEMUserParam createEMUserParam) {
        try {
            createEMUserParam.check();
            return emUserService.create(createEMUserParam.getUsername(), createEMUserParam.getPassword());
        } catch (IllegalParamException e) {
            return ResultBuilder.failHSingle(e.getMessage());
        } catch (Exception e) {
            return ResultBuilder.failHSingle(e.getMessage());
        }
    }

    public HStringResult delete(@RequestBody DeleteEMUserParam deleteEMUserParam) {
        try {
            deleteEMUserParam.check();
            return emUserService.delete(deleteEMUserParam.getUsername());
        } catch (IllegalParamException e) {
            return ResultBuilder.failHSingle(e.getMessage());
        } catch (Exception e) {
            return ResultBuilder.failHSingle(e.getMessage());
        }
    }

    @Override
    public HCollectionResult<String> deleteAll() {
        try {
            return emUserService.deleteAll();
        } catch (IllegalParamException e) {
            return ResultBuilder.failHSCollection(ResponseEnum.SERVICE_ERROR);
        } catch (Exception e) {
            return ResultBuilder.failHSCollection(ResponseEnum.SERVICE_ERROR);
        }
    }

    @Override
    public HStringResult<EMUser> getEMUserInfo(GetEMUserInfoParam getEMUserInfoParam) {
        try {
            getEMUserInfoParam.check();
            return emUserService.getUserInfo(getEMUserInfoParam.getUsername());
        } catch (IllegalParamException e) {
            return ResultBuilder.failHSingle(e.getMessage());
        } catch (Exception e) {
            return ResultBuilder.failHSingle(e.getMessage());
        }
    }

    @Override
    public HStringResult<Boolean> isUserOnline(IsEMUserOnlineParam isEMUserOnlineParam) {
        try {
            isEMUserOnlineParam.check();
            return emUserService.isUserOnline(isEMUserOnlineParam.getUsername());
        } catch (IllegalParamException e) {
            return ResultBuilder.failHSingle(e.getMessage());
        } catch (Exception e) {
            return ResultBuilder.failHSingle(e.getMessage());
        }
    }

    @Override
    public HCollectionResult<String> listAllUsers() {
        try {
            return emUserService.listAllUsers();
        } catch (IllegalParamException e) {
            return ResultBuilder.failHSCollection(ResponseEnum.SERVICE_ERROR);
        } catch (Exception e) {
            return ResultBuilder.failHSCollection(ResponseEnum.SERVICE_ERROR);
        }
    }

    @Override
    public HCollectionResult listUsers(ListEMUsersParam listEMUsersParam) {
        try {
            return emUserService.listUsers(listEMUsersParam.getLimit(), listEMUsersParam.getCursor());
        } catch (IllegalParamException e) {
            return ResultBuilder.failHSCollection(ResponseEnum.SERVICE_ERROR);
        } catch (Exception e) {
            return ResultBuilder.failHSCollection(ResponseEnum.SERVICE_ERROR);
        }
    }

    @Override
    public HStringResult<Boolean> updateUserPassword(UpadteEMUserPasswordParam upadteEMUserPasswordParam) {
        try {
            upadteEMUserPasswordParam.check();
            return emUserService.updateUserPassword(upadteEMUserPasswordParam.getUsername(), upadteEMUserPasswordParam.getPassword());
        } catch (IllegalParamException e) {
            return ResultBuilder.failHSingle(e.getMessage());
        } catch (Exception e) {
            return ResultBuilder.failHSingle(e.getMessage());
        }
    }
}
